package com.zztl.infiniteBand.ui.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.os.Handler
import android.util.AttributeSet
import android.view.View
import com.zztl.infiniteBand.R


/**
 * @desc
 * @auth ${user}
 * @time 2018/9/13  16:34
 */
class DialView : View {

    lateinit var smallPaint: Paint  //内圆的画笔
    lateinit var largePaint: Paint  //外圆的画笔
    lateinit var hander: Handler

    var selectdColor: Int = 0
    var color: Int = 0
    var selectedCircleWidth = 0f
    var circleWidth = 0f
    var mCanvas: Canvas? = null
    var mRectF: RectF? = null
    var valueObjectAnimator: ValueAnimator? = null

    private var selectedProgress = -1f


    constructor(context: Context) : this(context, null, 0)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)


    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    fun init(context: Context, attrs: AttributeSet?) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.DialView)
        selectdColor = array.getColor(R.styleable.DialView_selected_circle_color, resources.getColor(R.color.color_007aff))
        color = array.getColor(R.styleable.DialView_circle_color, resources.getColor(R.color.color_EFF0F1))
        selectedCircleWidth = array.getDimension(R.styleable.DialView_selected_circle_width, context.resources.getDimension(R.dimen.dp_17))
        circleWidth = array.getDimension(R.styleable.DialView_selected_circle_width, context.resources.getDimension(R.dimen.dp_25))
        smallPaint = Paint().apply {
            isAntiAlias = true
            color = selectdColor
            strokeWidth = selectedCircleWidth
//            style = Paint.Style.STROKE
        }

        largePaint = Paint().apply {
            isAntiAlias = true
            color = this@DialView.color
            strokeWidth = circleWidth
//            style = Paint.Style.STROKE

        }
        /*      hander = object : Handler() {
                  override fun handleMessage(msg: Message?) {
                      if (msg?.what == 1) {
                          mCanvas?.drawArc(mRectF!!, 0f, selectedProgress, false, smallPaint)
                      }
                  }
              }*/
        array.recycle()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = View.MeasureSpec.getSize(widthMeasureSpec)
        setMeasuredDimension(width, width)
    }

    override fun onDraw(canvas: Canvas) {
        var centerX = width / 2
        var centerY = height / 2
        smallPaint.style = Paint.Style.STROKE
        largePaint.style = Paint.Style.STROKE
        canvas.save()
        canvas.rotate(-225f, centerX.toFloat(), centerY.toFloat())
//        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), width / 2.toFloat() - circleWidth, largePaint)
       val rect= RectF(circleWidth, circleWidth, width.toFloat()-circleWidth , height.toFloat()-circleWidth)
//       val rect= RectF(0f, 0f, width.toFloat() , height.toFloat())
        canvas.drawArc(rect, 0f, 270f, false, largePaint)

        if (selectedProgress > 0) {

            canvas.rotate(270f,centerX.toFloat(), centerY.toFloat())
//            val rectF = RectF(circleWidth, circleWidth, width.toFloat() - circleWidth, height.toFloat() - circleWidth)
//            val rectF = RectF(selectedCircleWidth, selectedCircleWidth, width.toFloat() - selectedCircleWidth, height.toFloat() - selectedCircleWidth)
            /*  mCanvas = canvas
              mRectF = rectF
              hander.sendEmptyMessageDelayed(1, 500)*/
            canvas.drawArc(rect, 0f,-selectedProgress, false, smallPaint)
          /*
//            canvas.drawCircle(width - circleWidth, width / 2f, selectedCircleWidth / 2, smallPaint)*/
          /*  val x = (width / 2 + Math.cos(-selectedProgress / 180 * Math.PI) * (width / 2 - circleWidth)).toFloat()
            val y = (width / 2 + Math.sin(-selectedProgress / 180 * Math.PI) * (width / 2 - circleWidth)).toFloat()
            smallPaint.style = Paint.Style.FILL
            canvas.drawCircle(x, y, selectedCircleWidth / 2, smallPaint)*/
            canvas.restore()
        }


    }


    fun startAnimator() {
        if(null==valueObjectAnimator) {
            valueObjectAnimator = ValueAnimator.ofFloat(0f, selectedProgress).apply {
                duration = (2000 * selectedProgress / 360).toLong()
                addUpdateListener {
                    //                canvas.drawArc(rectF, 0f, it.animatedValue as Float, false, smallPaint)
                    selectedProgress = it.animatedValue as Float
                    postInvalidate()
                }
                start()
            }
        }else{
            valueObjectAnimator?.run {
                setFloatValues(0f,selectedProgress)
                duration= (1500 * selectedProgress / 360).toLong()
                start()
            }
        }

    }


    fun setProgress(progress: Float) {
        if(-1f!=selectedProgress&&selectedProgress==360 * progress){
            return
        }
        selectedProgress = 360 * progress

        startAnimator()
    }

    override fun onDetachedFromWindow() {
        valueObjectAnimator?.run {
            cancel()
        }
        super.onDetachedFromWindow()
    }

}
